package com.sfang.common.extension;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.sfang.common.util.log.LogUtils;
import com.sfang.common.util.string.StringUtils;

import java.net.InetAddress;

public class ServerIPClassicConverter extends ClassicConverter {

    static String SERVER_IP;
    static String SERVER_HOSTNAME;

    @Override
    public String convert(ILoggingEvent iLoggingEvent) {

        if (StringUtils.isEmpty(SERVER_IP)) {
            InetAddress localInetAddress = LogUtils.getLocalInetAddress();

            if (localInetAddress != null) {
                SERVER_IP = localInetAddress.getHostAddress();
                SERVER_HOSTNAME = localInetAddress.getHostAddress();
            }
        }

        return SERVER_IP;
    }
}
